package com.ad.pet.message.record;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.message.adapter.RecordListAdapter;
import com.ad.pet.models.remote.message.Record;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by SEELE on 2019/7/3.
 */

public class BuinessRecordListActivity extends AppToolbarActivity implements RecordContract.View {
    @Inject
    RecordPresenter mRecordPresenter;
    @BindView(R.id.recycler_list)
    RecyclerView mRecyclerList;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mSwipeRefresh;
    private int page = 1;
    private String limit = "50";
    private RecordListAdapter mRecordListAdapter;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mToolbar.setTitle(getString(R.string.trans_record));
        mStatusLayoutManager.showContent();
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_buiness_record_list_layout;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mRecordPresenter.onCreate(this);


//        mRecyclerList.addItemDecoration(new SimpleDividerDecoration(mActivity, LyxUtils.dp2px(mActivity,3),R.color.bg_color));
        mRecyclerList.setLayoutManager(new LinearLayoutManager(mActivity));
        mRecordListAdapter = new RecordListAdapter(null);
        mRecyclerList.setAdapter(mRecordListAdapter);


    }

    @Override
    protected void initData() {
        mRecordPresenter.getRecordList(String.valueOf(page), limit);
    }

    @Override
    protected void initEvents() {

        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipeRefresh.setRefreshing(true);
                page=1;
                mRecordPresenter.getRecordList(String.valueOf(page),limit);
            }
        });


        mRecordListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                mRecordPresenter.getRecordList(String.valueOf(page),limit);
            }
        },mRecyclerList);

        mRecordListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Navigation.goRecord(mActivity,mRecordListAdapter.getItem(position));
            }
        });

    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return mRecordPresenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void getRecordListSuccess(List<Record> records) {
        mSwipeRefresh.setRefreshing(false);
        if (page == 1) {
            mRecordListAdapter.setNewData(records);
        } else {
            mRecordListAdapter.addData(records);
            mRecordListAdapter.loadMoreComplete();
        }
        if (records.size() <Integer.valueOf(limit)) {//CommonConstant.PAGE_COUNT
            //暂无更多数据
            mRecordListAdapter.loadMoreEnd();
        }
        if (records.size() == 0 && page == 1) {
            mRecordListAdapter.setEmptyView(R.layout.layout_empty);
        }
    }

}
